package com.mchat.wsserver.handler;

import com.mchat.wsserver.OnlineUserMap;
import io.netty.channel.ChannelFutureListener;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.handler.codec.http.websocketx.BinaryWebSocketFrame;
import io.netty.handler.codec.http.websocketx.CloseWebSocketFrame;
import io.netty.handler.codec.http.websocketx.TextWebSocketFrame;
import io.netty.handler.codec.http.websocketx.WebSocketCloseStatus;
import lombok.extern.slf4j.Slf4j;


@Slf4j
public class WebSocketFrameHandler extends ChannelInboundHandlerAdapter {
    private long id;

    public WebSocketFrameHandler(long id) {
        this.id = id;
    }

    /**
     * 接收一个BinaryWebSocketFrame <br/>
     * 1个字节表示版本 <br/>
     * 4个字节表示ServerOperation中定义的操作码，opcode为0表示是该操作的响应 <br/>
     * 4个字节标识请求和响应的序号 <br/>
     * 1个字节表示响应是否还有后续 <br/>
     * 后面的字节是一个protobuf <br/>
     * --------------+------------+--------+----------+------------------+ <br/>
     * | version(1)  |  opcode(4) | seq(4) | isEnd(1) |payload (proto)  | <br/>
     * --------------+------------+--------+----------+-----------------+ <br/>
     */
    @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg) {
        if (msg instanceof BinaryWebSocketFrame){

        }
    }

    @Override
    public void channelInactive(ChannelHandlerContext ctx) throws Exception {
        OnlineUserMap.remove(id, ctx.channel());
        log.info("断开了连接,id:{},channel:{}.",id,ctx.channel());
    }
}
